﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuneLog.GrandExchange.GraphView
{
    public class GraphData
    {
        public Dictionary<long, int> Daily
        {
            get;
            private set;
        }
        public Dictionary<long, int> Average
        {
            get;
            private set;
        }

        public GraphData(int id)
        {
            var client = new System.Net.WebClient();
            var rawJson = client.DownloadString("http://services.runescape.com/m=itemdb_rs/api/graph/"+id+".json");
            string[] rawData = rawJson.Replace("{\"daily\":{", string.Empty).Replace("},\"average\":{", "$").Replace("}}", string.Empty).Split('$');
            
            //Parse Daily data
            string[] rawDaily = rawData[0].Split(',');
            Dictionary<long, int> tempDaily = new Dictionary<long,int>();
            foreach (string part in rawDaily)
            {
                string[] data = part.Replace("\"", string.Empty).Split(':');
                tempDaily.Add(long.Parse(data[0]), int.Parse(data[1]));
            }

            string[] rawAverage = rawData[1].Split(',');
            Dictionary<long, int> tempAverage = new Dictionary<long, int>();
            foreach (string part in rawAverage)
            {
                string[] data = part.Replace("\"", string.Empty).Split(':');
                tempAverage.Add(long.Parse(data[0]), int.Parse(data[1]));
            }

            Daily = tempDaily;
            Average = tempAverage;
        }
    }
}
